<template>
  <a-row type="flex" style="flex-flow: inherit;">
    <a-col :flex="toggle ? '260px' : '30px'" style="margin-right: 12px;background-color: #fff;">
      <depart-tree :rootOpened="true" :multi="false" @ok="getTreeData" @toggleBool="toggleBool" />
      <!-- <groups-tree @tree="getTreeData" @selectedKeys="getSelectedKeys" /> -->
    </a-col>
    <a-col flex="auto">
      <a-card :bordered="false" style="margin-bottom: 10px">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper" style="position: relative">
          <div class="search-wrapper-btn">
            <a-button type="link" @click="show = !show">
              <span v-if="!show">展开
                <a-icon type="down" />
              </span>
              <span v-else>收起
                <a-icon type="up" />
              </span>
            </a-button>
          </div>
          <a-form-model :label-col="labelCol" :wrapper-col="wrapperCol" @keyup.enter.native="onQuery">
            <div class="mode_item">
              <div class="mode_item_div">
                <a-form-model-item label="人员名称">
                  <a-input v-model="pageFilter.realName" placeholder="请输入" />
                </a-form-model-item>
              </div>
              <div class="mode_item_div">
                <a-form-model-item label="人员类型">
                  <a-select v-model="pageFilter.personType" placeholder="请选择" style="width:100%;">
                    <a-select-option value="0">全部</a-select-option>
                    <a-select-option v-for="(item, key) in personTypeArr" :key="key" :value="item.value">{{ item.title }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </div>
              <div class="mode_item_div">
                <a-form-model-item label="评估类型">
                  <a-select v-model="pageFilter.caseType" placeholder="请选择" allowClear>
                    <a-select-option value="0">全部</a-select-option>
                    <a-select-option v-for="(item, key) in caseTypeArr" :key="key" :value="item.value">{{ item.title }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </div>
            </div>
            <div class="mode_item" v-show="show">
              <div class="mode_item_div">
                <a-form-model-item label="评估时间">
                  <a-range-picker v-model="evalTime" allowClear valueFormat="YYYY-MM-DD" :disabled="isDisabled"
                    @change="onTimeChange" style="width:100%" />
                </a-form-model-item>
              </div>
              <div class="mode_item_div mode_item_flex">
                <a-form-model-item label="状态">
                  <a-radio-group v-model="pageFilter.evalStatus" @change="onChange">
                    <a-radio value="">全部</a-radio>
                    <a-radio v-for="(item, key) in evalStatusArr" :key="key" :value="item.value">{{ item.title }}
                    </a-radio>
                  </a-radio-group>
                </a-form-model-item>
              </div>
            </div>
            <div class="btnBox">
              <a-form-model-item :wrapper-col="{ offset: 0 }" style="margin-bottom: 0">
                <a-button type="primary" @click="onQuery"> 查询 </a-button>
                <a-button style="margin-left: 10px" @click="onResetForm"> 重置 </a-button>
              </a-form-model-item>
            </div>
          </a-form-model>
        </div>
      </a-card>
      <a-card :bordered="false">
        <!-- 操作按钮区域 -->
        <div class="table-operator">
          <a-button @click="onOut()">导出</a-button>
        </div>
        <!-- 表格展示 -->
        <div class="table-page-content">
          <z-table ref="table" rowKey="id" :showAlert="false" :showSelection="false" :loading="loading"
            :columns="columns" :dataSource="pageData" @change="onTableChange">
            <template slot="personType" slot-scope="text">
              <span>{{ setTittle1(text) }}</span>
            </template>
            <template slot="evalStatus" slot-scope="text">
              <!-- <span>{{ setTittle2(text) }}</span> -->
              <span v-if="text == '0'" style="color:#27BF44">待评估</span>
              <span v-if="text == '1'" style="color:#108EE9">待审批</span>
              <span v-if="text == '2'">已评估</span>
              <span v-if="text == '3'" style="color:#D9001B">已驳回</span>
              <span v-if="text == '4'" style="color:#ccc">已撤销</span>
            </template>
            <template slot="sjskDate" slot-scope="text, record">
              <span>{{ record.sjskStartDate }} ~ {{ record.sjskEndDate }}</span>
            </template>
            <!--操作-->
            <template slot="action" slot-scope="text, record">
              <template v-if="record.evalStatus == '0'">
                <a-button @click="onAdd(record, 1)" type="link" style="padding: 0">添加结案评估</a-button>
                <!-- <a-divider type="vertical" /> -->
              </template>
              <template v-if="record.evalStatus == '1'">
                <a-button @click="onCYX(record)" type="link" style="padding: 0">催一下
                </a-button>
                <a-divider type="vertical" />
              </template>
              <template v-if="record.evalStatus == '3' || record.evalStatus == '4'">
                <a-button @click="onAdd(record, 2)" type="link" style="padding: 0">重新提交</a-button>
                <a-divider type="vertical" />
              </template>
              <template v-if="record.evalStatus == '1'">
                <a-button @click="onCX(record)" type="link" style="padding: 0">撤销申请
                </a-button>
                <a-divider type="vertical" />
              </template>
              <a-button v-if="record.evalStatus != '0'" @click="onDetail(record)" type="link" style="padding: 0">查看
              </a-button>
            </template>
          </z-table>
        </div>
      </a-card>
    </a-col>
    <AddModal ref="addModalId" @ok="onQuery" />
  </a-row>
</template>

<script>
import AddModal from './modules/AddModal'
import DepartTree from '@/views/sjsk/modules/departTree'
import { downLoadFileReader } from '@/utils/export-util/util'
import { getDictItemsFromCache } from '@/api/api'
import * as caseEstimateApi from '@/api/sjsk/caseEstimate.js'
import { getUrge } from '@/api/sjsk/flowService'
export default {
  components: {
    DepartTree, AddModal
  },
  data() {
    return {
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      evalTime: [],
      show: false,
      loading: false,
      visible: false,
      reloading: false,
      treeDatas: [],
      toggle: true,
      personTypeArr: getDictItemsFromCache('userType'),
      caseTypeArr: getDictItemsFromCache('closing_type'),
      evalStatusArr: getDictItemsFromCache('closing_eval_status'),
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '姓名',
          dataIndex: 'realName',
          align: 'center',
        },
        {
          title: '身份证',
          dataIndex: 'identityCard',
          align: 'center',
        },
        {
          title: '人员类型',
          align: 'center',
          dataIndex: 'personType',
          scopedSlots: { customRender: 'personType' },
        },
        {
          title: '执行时间',
          dataIndex: 'sjskDate',
          align: 'center',
          scopedSlots: { customRender: 'sjskDate' },
        },
        {
          title: '状态',
          dataIndex: 'evalStatus',
          align: 'center',
          scopedSlots: { customRender: 'evalStatus' },
        },
        {
          title: '评估时间',
          dataIndex: 'evalTime',
          align: 'center',
        },
        {
          title: '操作',
          width: 250,
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
      },
      pageData: {},
      isDisabled: true,
      departmentId: "",
      // jsonParam: this.$route.query.jsonParam
    }
  },
  created() { },
  mounted() {
    // if (this.jsonParam) { //重新申请,弹框
    //   console.log("11111111")
    //   console.log(this.jsonParam)
    //   this.$refs.addModalId.show(this.jsonParam, 2)
    // }
  },
  // watch: {
  //   $route(data) {   //data为当前路由信息
  //     console.log("2222222222")
  //     if (data.name == 'caseEstimate' && this.jsonParam) {
  //       this.$refs.addModalId.show(this.jsonParam, 2)
  //     }
  //   }
  // },
  methods: {
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    onResetForm() {
      this.pageFilter = {
        pageNo: 1,
        pageSize: 10,
        departmentId: this.departmentId
      }
      this.isDisabled = true
      this.evalTime = []
      this.refreshTable()
    },
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = { currentPage: 'pageNo' }
      let paginations = Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations)
      this.refreshTable()
    },
    refreshTable() {
      this.loading = true
      caseEstimateApi.getPersons(this.pageFilter).then((res) => {
        if (res.success) {
          this.pageData = res.result ? res.result : {}
          this.loading = false
        } else {
          this.pageData = {}
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    onChange(e) {
      if (e.target.value == '2') {
        this.isDisabled = false
      } else {
        this.isDisabled = true
      }
    },
    setTittle1(text) {
      return this.$filterDictTextByCache('userType', text)  //现在字典中对应的文字，是全局函数
    },
    setTittle2(text) {
      return this.$filterDictTextByCache('closing_eval_status', text)  //现在字典中对应的文字，是全局函数
    },
    onTimeChange(date) {
      this.$set(this.pageFilter, 'evalStartTime', date[0])
      this.$set(this.pageFilter, 'evalEndTime', date[1])
    },
    onOut() {
      caseEstimateApi.exportCeList(this.pageFilter).then(async res => {
        const fileName = '结案评估人员.xls'
        await downLoadFileReader(res, fileName)
      })
    },
    onAdd(record, type) {
      // console.log(record);
      this.$refs.addModalId.show(record, type)
    },
    //跳转到审批流程
    onDetail(record) {
      this.$router.push({
        path: `/examine/details/16/${record.ceId}` //结案评估审批类型未16
      })
    },
    onCYX(record) {
      // getUrge({ taskId: record.id, taskType: '19' }).then((res) => { //取消常规尿液检测审批
      //   if (res.success) {
      //     this.$message.success(res.message);
      //     this.onQuery()
      //   } else {
      //     this.$message.warning(res.message);
      //   }
      // });
    },
    onCX(record) {
      let that = this;
      this.$confirm({
        title: `还在审批中，确定要撤销吗?`,
        okText: "确认",
        okType: "danger",
        cancelText: "取消",
        onOk() {
          caseEstimateApi.cancel({ taskId: record.ceId, type: "30" }).then((res) => { //结案评估审批流程类型为：30
            if (res.success) {
              that.$message.success(res.message);
              that.onQuery();
            } else {
              that.$message.warning(res.message);
            }
          });
        }
      });
    },
    /** 重新加载组件 */
    reload() {
      this.reloading = true
      this.$nextTick(() => (this.reloading = false))
    },
    // 选中组织架构树，赋值到人员列表搜索框
    getTreeData(datas, id) {
      if (id != undefined && id != null && id != "") {
        this.departmentId = id
        this.$set(this.pageFilter, 'departmentId', id)
        this.onQuery()
      }
    },
    toggleBool(bool) {
      console.log(bool)
      this.toggle = bool
    }
  },
}
</script>
<style lang="less" scoped>
.mode_item {
  display: flex;
  border-bottom: 1px #dadada dashed;

  .mode_item_div {
    width: 27%;
    padding: 5px 0;

    .ant-form-item {
      margin-bottom: 0;
    }
  }

  &>* {
    width: 100%;
    margin-bottom: 0;
    padding: 10px 0;
  }
}

.mode_item_flex {
  width: 72% !important;

  /deep/ .ant-col-6 {
    width: 9.8% !important;
  }
}

.search-wrapper-btn {
  position: absolute;
  right: 14%;
  top: 9px;
  z-index: 100;
}

.btnBox {
  position: absolute;
  right: 0;
  top: 5px;
  z-index: 120;
}

@import '~@assets/less/common.less';
</style>
